package cn.afactory.tygz.model;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.common.ErrorCode;

/**
 * Created with Android Studio.
 * Title:UploadFileInfo
 * Description:
 * Copyright:Copyright (c) 2015
 * Company:和平必胜、正义必胜、人民必胜
 * Author:炎黄子孙
 * Date:15/9/14
 * Time:下午12:54
 * Version 1.0
 */
public class UploadFileInfoList extends BaseDataHeader implements Serializable {

    public List<UploadFileInfo> data;

    public int count = 0;
    private static final long serialVersionUID = -6006381141981728099L;
    public static UploadFileInfoList deserialize(String json) throws JSONException {
        if (json != null && !json.isEmpty()) {
            return deserialize(new JSONObject(json));
        }
        return null;
    }

    public static UploadFileInfoList deserialize(JSONObject json) throws JSONException {
        if (json != null && json != JSONObject.NULL && json.length() > 0) {
            UploadFileInfoList result = new UploadFileInfoList();
            result.message = json.optString("message");
            result.state = json.optInt("state");
            JSONArray filesArray = json.optJSONArray("data");
            if(result.state == ErrorCode.STATUS_OK) {
                if (filesArray != null) {
                    int len = filesArray.length();
                    result.data = new ArrayList<UploadFileInfo>(len);
                    for (int i = 0; i < len; i++) {
                        JSONObject jo = filesArray.optJSONObject(i);
                        if (jo != null && jo != JSONObject.NULL) {
                            result.data.add(UploadFileInfo.deserialize(jo));
                        }
                    }
                }
            }
            return result;
        }
        return null;
    }

}
